home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / progs / sviluppo / python-1.4 / lib / test / test_pow.py < prev    next >
Text File  |  1997-01-12  |  3KB  |  98 lines

  1. import sys
  2.  
  3. eps = 1e-9
  4.  
  5. def powtest(type):
  6.     if (type!=float): 
  7.         print "    Testing 2-argument pow() function..."
  8.         for i in range(-1000, 1000):
  9.             if (pow(type(i),0)!=1): 
  10.                 raise ValueError, 'pow('+str(i)+',0) != 1'
  11.             if (pow(type(i),1)!=type(i)):
  12.                 raise ValueError, 'pow('+str(i)+',1) != '+str(i)
  13.             if (pow(type(0),1)!=type(0)):
  14.                 raise ValueError, 'pow(0,'+str(i)+') != 0'
  15.             if (pow(type(1),1)!=type(1)):
  16.                 raise ValueError, 'pow(1,'+str(i)+') != 1'
  17.         
  18.         for i in range(-100, 100):
  19.             if (pow(type(i),3)!=i*i*i):
  20.                 raise ValueError, 'pow('+str(i)+',3) != '+str(i*i*i)
  21.     
  22.         pow2=1
  23.         for i in range(0,31):
  24.             if (pow(2,i)!=pow2):
  25.                 raise ValueError, 'pow(2,'+str(i)+') != '+str(pow2)
  26.             if (i!=30): pow2=pow2*2
  27.  
  28.     print "    Testing 3-argument pow() function..."
  29.     il, ih = -20, 20
  30.     jl, jh = -5,   5
  31.     kl, kh = -10, 10
  32.     if (type==float):
  33.         il=1
  34.     elif (type==int):
  35.         jl=0
  36.     elif (type==long):
  37.         jl,jh = 0, 15
  38.     for i in range(il, ih+1):
  39.          for j in range(jl,jh+1):
  40.              for k in range(kl, kh+1):
  41.                    if (k!=0):
  42.              # if (pow(type(i),j,k) != pow(type(i),j)% type(k) ):
  43.              if (abs(pow(type(i),j,k)-(pow(type(i),j)% type(k)))>eps):
  44.                          raise ValueError, "pow(" +str(i)+ "," +str(j)+ \
  45.                   "," +str(k)+ ") != pow(" +str(i)+ "," + \
  46.                   str(j)+ ") % " +str(k)
  47.  
  48.  
  49. print 'Testing integer mode...'
  50. powtest(int)
  51. print 'Testing long integer mode...'
  52. powtest(long)
  53. print 'Testing floating point mode...'
  54. powtest(float)
  55.  
  56. # Other tests-- not very systematic
  57.  
  58. print 'The number in both columns should match.'
  59. print pow(3,3) % 8, pow(3,3,8)
  60. print pow(3,3) % -8, pow(3,3,-8)
  61. print pow(3,2) % -2, pow(3,2,-2)
  62. print pow(-3,3) % 8, pow(-3,3,8)
  63. print pow(-3,3) % -8, pow(-3,3,-8)
  64. print pow(5,2) % -8, pow(5,2,-8)
  65. print
  66.  
  67. print pow(3L,3L) % 8, pow(3L,3L,8)
  68. print pow(3L,3L) % -8, pow(3L,3L,-8)
  69. print pow(3L,2) % -2, pow(3L,2,-2)
  70. print pow(-3L,3L) % 8, pow(-3L,3L,8)
  71. print pow(-3L,3L) % -8, pow(-3L,3L,-8)
  72. print pow(5L,2) % -8, pow(5L,2,-8)
  73. print
  74.  
  75. print pow(3.0,3.0) % 8, pow(3.0,3.0,8)
  76. print pow(3.0,3.0) % -8, pow(3.0,3.0,-8)
  77. print pow(3.0,2) % -2, pow(3.0,2,-2)
  78. print pow(5.0,2) % -8, pow(5.0,2,-8)
  79. print
  80.  
  81. print "testing..."
  82.  
  83. for i in range(-10, 11):
  84.  for j in range(0, 6):
  85.   for k in range(-7, 11):
  86.    if (j>=0 and k!=0):
  87.     o=pow(i,j) % k
  88.     n=pow(i,j,k)
  89.     if (o!=n): print 'Integer mismatch:', i,j,k
  90.    if (j>=0 and k<>0):
  91.     o=pow(long(i),j) % k
  92.     n=pow(long(i),j,k)
  93.     if (o!=n): print 'Long mismatch:', i,j,k
  94.    if (i>=0 and k<>0):
  95.      o=pow(float(i),j) % k
  96.      n=pow(float(i),j,k)
  97.      if (o!=n): print 'Float mismatch:', i,j,k
  98.